package ${serviceInterfacePackageName}.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import ${packageName}.${className}Example;
import ${packageName}.${className}Example.Criteria;
import ${mapperPackageName}.${className}Mapper;
import ${packageName}.${className};
import ${serviceInterfacePackageName}.${className}Service;

import com.pyg.utils.PygStringUtils;
import entity.PageResult;

/**	<#--定义常用变量 类名小写 mapper类小写-->
	<#assign classInstance = className?uncap_first/>
 	<#assign mapperInstance = className?uncap_first + 'Mapper'/>
 	<#assign idtype='String' />
	<#list attrs as attr>
	<#if attr.name=="id" >
	<#assign idtype=attr.type />
	</#if>
	</#list>
*  @Author ${author}
*  @Email ${email}
*  @Date ${date?string("yyyy-MM-dd HH:mm:ss")}
*/
@Service
public class ${className}ServiceImpl implements ${className}Service {
	@Autowired
	private ${className}Mapper ${mapperInstance};

	@Override
	public List<${className}> findAll() {
		return ${mapperInstance}.selectByExample(null);
	}

	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		// 开启分页插件功能
		PageHelper.startPage(pageNum, pageSize);

		Page<${className}> page = (Page<${className}>) ${mapperInstance}.selectByExample(null);

		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 品牌条件分页
	 *
	 * @param ${className}
	 *            查询条件
	 * @param pageNum
	 *            当前页面
	 * @param pageSize
	 *            每页记录数
	 * @return
	 */
	@Override
	public PageResult findPage(${className} ${classInstance}, int pageNum, int pageSize) {
		// 开启分页插件功能
		PageHelper.startPage(pageNum, pageSize);

		${className}Example example = null;
		if (${classInstance} != null) {
			example = new ${className}Example();
			Criteria criteria = example.createCriteria();
			<#-- 属性条件-->
			<#list attrs as attr>
			<#if attr.name!="id" >	
			if (PygStringUtils.isNotEmpty(${classInstance}.get${attr.name?cap_first}())) {
				<#if attr.type=="String" >	
				criteria.and${attr.name?cap_first}Like("%" + ${classInstance}.get${attr.name?cap_first}() + "%");
				<#else>
				criteria.and${attr.name?cap_first}EqualTo(${classInstance}.get${attr.name?cap_first}() );
				</#if>
			}
			</#if>
			</#list>
		}

		Page<${className}> page = (Page<${className}>) ${mapperInstance}.selectByExample(example);

		return new PageResult(page.getTotal(), page.getResult());
	}

	@Override
	public void add(${className} ${classInstance}) {
		${mapperInstance}.insert(${classInstance});
	}

	@Override
	public void del(${idtype}[] ids) {
		for (${idtype} id : ids) {
			${mapperInstance}.deleteByPrimaryKey(id);
		}
	}

	@Override
	public ${className} findOne(${idtype} id) {
		return ${mapperInstance}.selectByPrimaryKey(id);
	}

	@Override
	public void update(${className} ${classInstance}) {
		${mapperInstance}.updateByPrimaryKey(${classInstance});
	}

}
